package com.cnunicom.modules.tra.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnunicom.common.utils.Constant;
import com.cnunicom.modules.tra.vo.UnitMebHisVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;

import com.cnunicom.modules.tra.dao.UnitDao;
import com.cnunicom.modules.tra.entity.UnitEntity;
import com.cnunicom.modules.tra.service.UnitService;

@Slf4j
@Service("unitService")
public class UnitServiceImpl extends ServiceImpl<UnitDao, UnitEntity> implements UnitService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String unitName = (String) params.get("unitName");
        IPage<UnitEntity> page = this.page(
                new Query<UnitEntity>().getPage(params,"create_time",false),
                new QueryWrapper<UnitEntity>()
                .like("name",unitName)
        );

        return new PageUtils(page);
    }

    @Override
    public List<Object> queryUnitMebHisPage(Map<String, Object> params) {

        UnitMebHisVO vo = voHandler(params);
        IPage<UnitMebHisVO> page = new Query<UnitMebHisVO>()
                .getPage(params, "create_time", false);
        List<UnitMebHisVO> vos = this.baseMapper.queryUnitMebHisPage((Page) params.get(Constant.PAGE), vo);

        page.setRecords(vos);

        Map<String, String> map = totalHandler(vos);

        PageUtils pageUtils = new PageUtils(page);
        List<Object> objects = new ArrayList<>();

        objects.add(pageUtils);
        objects.add(map);

        return objects;
    }

    /**
     * 总金额处理器
     * @param vos
     * @return
     */
    private Map<String,String> totalHandler(List<UnitMebHisVO> vos) {
        //总人数
        Integer totalMeb = vos.stream().map(UnitMebHisVO::getTotalMember).reduce(Integer::sum).orElse(0);
        //总价格
        BigDecimal totalPrice = vos.stream().map(UnitMebHisVO::getTotalPrice).reduce(BigDecimal::add)
                .orElse(BigDecimal.ZERO);
        //房间总数
        Long totalStay = vos.stream().map(UnitMebHisVO::getTotalStay).reduce(Long::sum).orElse(0L);

        HashMap<String, String> map = new HashMap<>();
        map.put("totalMeb", totalMeb.toString());
        map.put("totalPrice", totalPrice.toString());
        map.put("totalStay", totalStay.toString());

        return map;
    }

    private UnitMebHisVO voHandler(Map<String, Object> params) {
        UnitMebHisVO vo = new UnitMebHisVO();
        String name = (String)params.get("unitName");
        vo.setName(name);
        return vo;
    }

    @Override
    public Boolean batchSave(List<UnitEntity> unitList) {
        log.info("UnitService {}条数据，开始存储数据库", unitList.size());
        this.saveBatch(unitList);
        log.info("存储数据成功");
        return Boolean.TRUE;
    }

    private UnitEntity getUnitEntity(Map<String,Object> params) {
        UnitEntity entity = new UnitEntity();
        String unitName = (String) params.get("unitName");

        if (StringUtils.isNotBlank(unitName)) {
            entity.setName(unitName);
        }

        return entity;
    }
}
